// VS_BEGIN
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aCoord;
layout (location = 2) in vec3 aNormal;

uniform mat4 u_model;

layout (std140) uniform EngineUBO
{
    mat4 u_view;
    mat4 u_project;
    vec3 u_camPos;
};

void main() {
    gl_Position = u_model * vec4(aPos, 1.0f);
}

// VS_END

// GS_BEGIN
#version 330 core
layout (triangles) in;
layout (triangle_strip, max_vertices = 18) out;

// struct ShadowMat 
// {
//     mat4 matrix;
// };
// uniform ShadowMat u_shadowMat[6];
uniform mat4 u_shadowMat[6];

out vec4 g_worldPos;

void main() {
    for (int face = 0; face < 6; ++face) {
        gl_Layer = face;
        for (int i = 0; i < 3; ++i) {
            g_worldPos = gl_in[i].gl_Position;
            gl_Position = u_shadowMat[face] * g_worldPos;
            EmitVertex();
        }
        EndPrimitive();
    }
}
// GS_END


// FS_BEGIN
#version 330 core

in vec4 g_worldPos;

uniform vec3 u_lightPos;
uniform float u_zFar;

void main() {
    float dis = length(g_worldPos.xyz - u_lightPos);
    gl_FragDepth = dis / u_zFar;
}
// FS_END